import axios from 'axios'
import { ElMessage } from 'element-plus'
import { userStore } from '@/stores/user.js'

const UserStore = userStore()
const token = UserStore.userInfo.token
const instance = axios.create({
  baseURL: 'http://localhost:8080'
})

instance.interceptors.request.use(
  (config) => {
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`
      config.headers['Content-Type'] = 'application/json'
    }
    return config
  },
  (e) => {
    ElMessage.error('请先登录')
    return Promise.reject(e)
  }
)

instance.interceptors.response.use(
  (response) => response.data,
  (e) => Promise.reject(e.message)
)
export default instance
